<template>
  <view>
    <uni-swipe-action >
      <view style="margin-top: 30rpx" v-for="(msg,index) in msgs" :key="index">
      <uni-swipe-action-item  :disabled="msg.status=='已读'" :right-options="options2" @click="bindClick($event, index)">
        <uni-card :title="msg.type" :sub-title="fromDate(msg.createDatetime)" :isFull="true"
                  :extra="msg.status">
          <text>{{ msg.content }}</text>
        </uni-card>
      </uni-swipe-action-item>
      </view>
    </uni-swipe-action>
    <uni-load-more v-if="queryParams.pageSize<msgs.length" color="#007AFF" :status="status" />
  </view>
</template>

<script>
import {MessageService} from "./messageService";

export default {
  data() {
    return {
      options2: [
        {
          text: '已读',
          style: {
            backgroundColor: '#007aff'
          }
        },
      ],
      msgs: [],
      user:uni.getStorageSync('userInfo').userInfo,
      queryParams:{
        currentPage:1,
        pageSize:10,
        filters:{
          toUserId:uni.getStorageSync('userInfo').eid,
        },
        orderBys:[
          {
            property: "createDatetime",
            order: "desc"
          }
        ]
      },
      status:'more',
      totalCount:100,
    }
  },
  created(){
    let socket=this.$store.state.socket
    let subscribe="monitorMessage-"+this.user.eid
    socket.stompSubscribe(subscribe,(destination, body, headers)=>{
      this.msgs=[body,...this.msgs]
    })
    this.init(0)
  },
  onUnload() {
    let socket=this.$store.state.socket
    let subscribe="monitorMessage-"+this.user.eid
    socket.stompUnsubscribe(subscribe);
  },
  onReachBottom(){
    //触底业务逻辑
    if(this.queryParams.pageSize*this.queryParams.currentPage>=this.totalCount){
      this.status="nomore"
      return;
    }
    this.queryParams.currentPage++;
    this.init(1);
  },
  methods: {
    init(type){
      this.status="loading"
      if(type==0){this.msgs=[];this.queryParams.currentPage=1}
      MessageService.findMessages(this.queryParams).then(res=>{
        this.msgs=this.msgs.concat( res.data.datas);
        // console.log(this.msgs)
        this.totalCount=res.data.totalCount;
        this.status='more'
      })
    },
    bindClick(e,i){
      // console.log(e,i)
      // console.log(this.msgs[i])
      if(e.content.text=='已读'){
        MessageService.readMessage(this.msgs[i].eid).then(res=>{
          if(res.data.code==0){
            this.msgs[i].status = "已读"
            uni.showToast({
              title:"已读成功"
            })
            let pages = getCurrentPages(); // 当前页面
            let beforePage = pages[pages.length - 2]; // 上一页
            beforePage.$vm.msgNum--;
          }
        }).catch(err=>{
          uni.showToast({
            title:"网络异常"
          })
        })
      }
    },
    fromDate(datetime){
      if(typeof datetime=='string'){
        return datetime;
      }
      var date = new Date(datetime);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
      var year = date.getFullYear(),
          month = ("0" + (date.getMonth() + 1)).slice(-2),
          sdate = ("0" + date.getDate()).slice(-2),
          hour = ("0" + date.getHours()).slice(-2),
          minute = ("0" + date.getMinutes()).slice(-2),
          second = ("0" + date.getSeconds()).slice(-2);
      // 拼接
      var result = year + "-"+ month +"-"+ sdate +" "+ hour +":"+ minute +":" + second;
      // 返回
      return result;
    }
  }
}
</script>

<style>

</style>
